--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Examples/Announce.py 85d77c10a11ff0e943b100ef86fb5cbfa791c8b3 (85d77c10) Text, 6.12 KB
T8b949e##########################################################
T8b949e# This RNS example demonstrates setting up announce #
T8b949e# callbacks, which will let an application receive a #
T8b949e# notification when an announce relevant for it arrives #
T8b949e##########################################################
Tff7b72import T7ee787argparse
Tff7b72import T7ee787random
Tff7b72import T7ee787sys
Tff7b72import T7ee787RNS
T8b949e# Let's define an app name. We'll use this for all
T8b949e# destinations we create. Since this basic example
T8b949e# is part of a range of example utilities, we'll put
T8b949e# them all within the app namespace "example_utilities"
Te6edf3APP_NAME Tff7b72= Ta5d6ff"Ta5d6ffexample_utilitiesTa5d6ff"
T8b949e# We initialise two lists of strings to use as app_data
Te6edf3fruits Tff7b72= Tb4b4b4[Ta5d6ff"Ta5d6ffPeachTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffQuinceTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffDateTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffTangerineTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffPomeloTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffCarambolaTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffGrapeTa5d6ff"Tb4b4b4]
Te6edf3noble_gases Tff7b72= Tb4b4b4[Ta5d6ff"Ta5d6ffHeliumTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffNeonTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffArgonTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffKryptonTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffXenonTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffRadonTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffOganessonTa5d6ff"Tb4b4b4]
T8b949e# This initialisation is executed when the program is started
Tff7b72def Td2a8ffprogram_setupTb4b4b4(Te6edf3configpathTb4b4b4)Tb4b4b4:
T8b949e# We must first initialise Reticulum
Te6edf3reticulum Tff7b72= Te6edf3RNSTff7b72.Td2a8ffReticulumTb4b4b4(Te6edf3configpathTb4b4b4)
T8b949e# Randomly create a new identity for our example
Te6edf3identity Tff7b72= Te6edf3RNSTff7b72.Td2a8ffIdentityTb4b4b4(Tb4b4b4)
T8b949e# Using the identity we just created, we create two destinations
T8b949e# in the "example_utilities.announcesample" application space.
T8b949e#
T8b949e# Destinations are endpoints in Reticulum, that can be addressed
T8b949e# and communicated with. Destinations can also announce their
T8b949e# existence, which will let the network know they are reachable
T8b949e# and automatically create paths to them, from anywhere else
T8b949e# in the network.
Te6edf3destination_1 Tff7b72= Te6edf3RNSTff7b72.Td2a8ffDestinationTb4b4b4(
Te6edf3identityTb4b4b4,
Te6edf3RNSTff7b72.Td2a8ffDestinationTff7b72.Td2a8ffINTb4b4b4,
Te6edf3RNSTff7b72.Td2a8ffDestinationTff7b72.Td2a8ffSINGLETb4b4b4,
Te6edf3APP_NAMETb4b4b4,
Ta5d6ff"Ta5d6ffannouncesampleTa5d6ff"Tb4b4b4,
Ta5d6ff"Ta5d6fffruitsTa5d6ff"
Tb4b4b4)
Te6edf3destination_2 Tff7b72= Te6edf3RNSTff7b72.Td2a8ffDestinationTb4b4b4(
Te6edf3identityTb4b4b4,
Te6edf3RNSTff7b72.Td2a8ffDestinationTff7b72.Td2a8ffINTb4b4b4,
Te6edf3RNSTff7b72.Td2a8ffDestinationTff7b72.Td2a8ffSINGLETb4b4b4,
Te6edf3APP_NAMETb4b4b4,
Ta5d6ff"Ta5d6ffannouncesampleTa5d6ff"Tb4b4b4,
Ta5d6ff"Ta5d6ffnoble_gasesTa5d6ff"
Tb4b4b4)
T8b949e# We configure the destinations to automatically prove all
T8b949e# packets addressed to it. By doing this, RNS will automatically
T8b949e# generate a proof for each incoming packet and transmit it
T8b949e# back to the sender of that packet. This will let anyone that
T8b949e# tries to communicate with the destination know whether their
T8b949e# communication was received correctly.
Te6edf3destination_1Tff7b72.Td2a8ffset_proof_strategyTb4b4b4(Te6edf3RNSTff7b72.Td2a8ffDestinationTff7b72.Td2a8ffPROVE_ALLTb4b4b4)
Te6edf3destination_2Tff7b72.Td2a8ffset_proof_strategyTb4b4b4(Te6edf3RNSTff7b72.Td2a8ffDestinationTff7b72.Td2a8ffPROVE_ALLTb4b4b4)
T8b949e# We create an announce handler and configure it to only ask for
T8b949e# announces from "example_utilities.announcesample.fruits".
T8b949e# Try changing the filter and see what happens.
Te6edf3announce_handler Tff7b72= Te6edf3ExampleAnnounceHandlerTb4b4b4(
Te6edf3aspect_filterTff7b72=Ta5d6ff"Ta5d6ffexample_utilities.announcesample.fruitsTa5d6ff"
Tb4b4b4)
T8b949e# We register the announce handler with Reticulum
Te6edf3RNSTff7b72.Td2a8ffTransportTff7b72.Td2a8ffregister_announce_handlerTb4b4b4(Te6edf3announce_handlerTb4b4b4)
T8b949e# Everything's ready!
T8b949e# Let's hand over control to the announce loop
Te6edf3announceLoopTb4b4b4(Te6edf3destination_1Tb4b4b4, Te6edf3destination_2Tb4b4b4)
Tff7b72def Td2a8ffannounceLoopTb4b4b4(Te6edf3destination_1Tb4b4b4, Te6edf3destination_2Tb4b4b4)Tb4b4b4:
T8b949e# Let the user know that everything is ready
Te6edf3RNSTff7b72.Td2a8fflogTb4b4b4(Ta5d6ff"Ta5d6ffAnnounce example running, hit enter to manually send an announce (Ctrl-C to quit)Ta5d6ff"Tb4b4b4)
T8b949e# We enter a loop that runs until the users exits.
T8b949e# If the user hits enter, we will announce our server
T8b949e# destination on the network, which will let clients
T8b949e# know how to create messages directed towards it.
Tff7b72while Tff7b72TrueTb4b4b4:
Te6edf3entered Tff7b72= Tffa657inputTb4b4b4(Tb4b4b4)
T8b949e# Randomly select a fruit
Te6edf3fruit Tff7b72= Te6edf3fruitsTb4b4b4[Te6edf3randomTff7b72.Td2a8ffrandintTb4b4b4(T79c0ff0Tb4b4b4,Tffa657lenTb4b4b4(Te6edf3fruitsTb4b4b4)Tff7b72-T79c0ff1Tb4b4b4)Tb4b4b4]
T8b949e# Send the announce including the app data
Te6edf3destination_1Tff7b72.Td2a8ffannounceTb4b4b4(Te6edf3app_dataTff7b72=Te6edf3fruitTff7b72.Td2a8ffencodeTb4b4b4(Ta5d6ff"Ta5d6ffutf-8Ta5d6ff"Tb4b4b4)Tb4b4b4)
Te6edf3RNSTff7b72.Td2a8fflogTb4b4b4(
Ta5d6ff"Ta5d6ffSent announce from Ta5d6ff"Tff7b72+
Te6edf3RNSTff7b72.Td2a8ffprettyhexrepTb4b4b4(Te6edf3destination_1Tff7b72.Td2a8ffhashTb4b4b4)Tff7b72+
Ta5d6ff"Ta5d6ff (Ta5d6ff"Tff7b72+Te6edf3destination_1Tff7b72.Td2a8ffnameTff7b72+Ta5d6ff"Ta5d6ff)Ta5d6ff"
Tb4b4b4)
T8b949e# Randomly select a noble gas
Te6edf3noble_gas Tff7b72= Te6edf3noble_gasesTb4b4b4[Te6edf3randomTff7b72.Td2a8ffrandintTb4b4b4(T79c0ff0Tb4b4b4,Tffa657lenTb4b4b4(Te6edf3noble_gasesTb4b4b4)Tff7b72-T79c0ff1Tb4b4b4)Tb4b4b4]
T8b949e# Send the announce including the app data
Te6edf3destination_2Tff7b72.Td2a8ffannounceTb4b4b4(Te6edf3app_dataTff7b72=Te6edf3noble_gasTff7b72.Td2a8ffencodeTb4b4b4(Ta5d6ff"Ta5d6ffutf-8Ta5d6ff"Tb4b4b4)Tb4b4b4)
Te6edf3RNSTff7b72.Td2a8fflogTb4b4b4(
Ta5d6ff"Ta5d6ffSent announce from Ta5d6ff"Tff7b72+
Te6edf3RNSTff7b72.Td2a8ffprettyhexrepTb4b4b4(Te6edf3destination_2Tff7b72.Td2a8ffhashTb4b4b4)Tff7b72+
Ta5d6ff"Ta5d6ff (Ta5d6ff"Tff7b72+Te6edf3destination_2Tff7b72.Td2a8ffnameTff7b72+Ta5d6ff"Ta5d6ff)Ta5d6ff"
Tb4b4b4)
T8b949e# We will need to define an announce handler class that
T8b949e# Reticulum can message when an announce arrives.
Tff7b72class T56d364ExampleAnnounceHandlerTb4b4b4:
T8b949e# The initialisation method takes the optional
T8b949e# aspect_filter argument. If aspect_filter is set to
T8b949e# None, all announces will be passed to the instance.
T8b949e# If only some announces are wanted, it can be set to
T8b949e# an aspect string.
Tff7b72def Tff7b72__init__Tb4b4b4(Tff7b72selfTb4b4b4, Te6edf3aspect_filterTff7b72=Tff7b72NoneTb4b4b4)Tb4b4b4:
Tff7b72selfTff7b72.Td2a8ffaspect_filter Tff7b72= Te6edf3aspect_filter
T8b949e# This method will be called by Reticulums Transport
T8b949e# system when an announce arrives that matches the
T8b949e# configured aspect filter. Filters must be specific,
T8b949e# and cannot use wildcards.
Tff7b72def Td2a8ffreceived_announceTb4b4b4(Tff7b72selfTb4b4b4, Te6edf3destination_hashTb4b4b4, Te6edf3announced_identityTb4b4b4, Te6edf3app_dataTb4b4b4)Tb4b4b4:
Te6edf3RNSTff7b72.Td2a8fflogTb4b4b4(
Ta5d6ff"Ta5d6ffReceived an announce from Ta5d6ff"Tff7b72+
Te6edf3RNSTff7b72.Td2a8ffprettyhexrepTb4b4b4(Te6edf3destination_hashTb4b4b4)
Tb4b4b4)
Tff7b72if Te6edf3app_dataTb4b4b4:
Te6edf3RNSTff7b72.Td2a8fflogTb4b4b4(
Ta5d6ff"Ta5d6ffThe announce contained the following app data: Ta5d6ff"Tff7b72+
Te6edf3app_dataTff7b72.Td2a8ffdecodeTb4b4b4(Ta5d6ff"Ta5d6ffutf-8Ta5d6ff"Tb4b4b4)
Tb4b4b4)
T8b949e##########################################################
T8b949e#### Program Startup #####################################
T8b949e##########################################################
T8b949e# This part of the program gets run at startup,
T8b949e# and parses input from the user, and then starts
T8b949e# the desired program mode.
Tff7b72if Tff7b72__name__ Tff7b72== Ta5d6ff"Ta5d6ff__main__Ta5d6ff"Tb4b4b4:
Tff7b72tryTb4b4b4:
Te6edf3parser Tff7b72= Te6edf3argparseTff7b72.Td2a8ffArgumentParserTb4b4b4(
Te6edf3descriptionTff7b72=Ta5d6ff"Ta5d6ffReticulum example that demonstrates announces and announce handlersTa5d6ff"
Tb4b4b4)
Te6edf3parserTff7b72.Td2a8ffadd_argumentTb4b4b4(
Ta5d6ff"Ta5d6ff--configTa5d6ff"Tb4b4b4,
Te6edf3actionTff7b72=Ta5d6ff"Ta5d6ffstoreTa5d6ff"Tb4b4b4,
Te6edf3defaultTff7b72=Tff7b72NoneTb4b4b4,
Te6edf3helpTff7b72=Ta5d6ff"Ta5d6ffpath to alternative Reticulum config directoryTa5d6ff"Tb4b4b4,
Tffa657typeTff7b72=Tffa657str
Tb4b4b4)
Te6edf3args Tff7b72= Te6edf3parserTff7b72.Td2a8ffparse_argsTb4b4b4(Tb4b4b4)
Tff7b72if Te6edf3argsTff7b72.Td2a8ffconfigTb4b4b4:
Te6edf3configarg Tff7b72= Te6edf3argsTff7b72.Td2a8ffconfig
Tff7b72elseTb4b4b4:
Te6edf3configarg Tff7b72= Tff7b72None
Te6edf3program_setupTb4b4b4(Te6edf3configargTb4b4b4)
Tff7b72except Tf85149KeyboardInterruptTb4b4b4:
Tffa657printTb4b4b4(Ta5d6ff"Ta5d6ff"Tb4b4b4)
Te6edf3sysTff7b72.Td2a8ffexitTb4b4b4(T79c0ff0Tb4b4b4)
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────